<?php
namespace app\admin\controller\nursinguser;;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use think\App;
/**
 * @ControllerAnnotation(title="选项设置")
 * Class Optiontype
 */
class Optiontype extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'nursing_lang_id',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\OptionType();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function index()
    {
        $param = request()->param();
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames([],$this->AllData['field']??[]);
            if(isset($this->AllData['is_page'])&&$this->AllData['is_page']==0){
                $limit = 10000;
            }

            $where = PublicUse::getParameter($where);
            $nursing_lang_id = $this->request->param('nursing_lang_id');
            $nursing_lang = \app\admin\model\NursingLang::where('id',$nursing_lang_id)
                ->find();
            if(!empty($nursing_lang)){
                if($nursing_lang->type_id==3){
                    foreach ($where as $k=>$v){
                        if($v[0]=='nursing_lang_id'){
                            $v[2] = 8;
                        }
                        $where[$k] = $v;
                    }
                }
            }
//            dump($where);die();
//            $where[] = ['id','=',16];
            $count = $this->model
                ->where($where)
                ->count();
            try{
                $list = $this->model
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
            }catch (\Exception $e){
                $list = $this->model
                    ->where($where)
                    ->page($page, $limit)
//                ->order($this->sort)
                    ->select();
            }
//            dump($list->toArray());die();
            $list = PublicUse::objctToModelAndField($list,$this->AllData['field']??[]);
//            dump($list);die();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $parameter = '';
        $fi = $this->model->getPk();
        if(!empty($param['field'])&&!empty($param[$fi])){
            $parameter = "?{$param['field']}={$param[$fi]}";
        }
        $newwhere = $this->request->get();
        foreach ($newwhere as $key=>$value){
            if($key!==$fi&&$key!=='field'){
                if(strpos($parameter,'?')===false){
                    $parameter .= "?{$key}={$value}";
                }else{
                    $parameter .= "&{$key}={$value}";
                }
            }
        }
//        echo $parameter;exit;
        $this->assign('parameter',$parameter);
        return $this->fetch();
    }

    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
//            echo '<pre>';
//            print_r($this->AllData['field']);
//            exit;
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            $this->assign('alldata',$this->AllData);
        }
        $nursing_lang_id = $this->request->param('nursing_lang_id');
        $nursing_lang = \app\admin\model\NursingLang::where('id',$nursing_lang_id)
            ->find();
        if(!empty($nursing_lang)){
            if($nursing_lang->type_id==3){
               $data['nursing_lang_id'] = 8;
            }
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
//        echo '<pre>';
//        print_r($data);
//        exit;
        $this->assign('row',$data);
        return $this->fetch();
    }
}